define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/index',
                    add_url: 'user/user/add',
                    edit_url: 'user/user/edit',
                    del_url: 'user/user/del',
                    multi_url: 'user/user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            //在普通搜索渲染后
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='parent_id']", form).addClass("selectpage").data("source", "user/user/index").data("primaryKey", "id").data("field", "username").data('format-item', '{username}/{id}').data("orderBy", "id desc");
            })
            //在普通搜索渲染后
            table.on('post-body.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                require(['../addons/bootstrapcontextmenu/js/bootstrap-contextmenu'], function (undefined) {

                    $("body").append(
                        '<div id="context-menu2">' +
                        '<ul class="dropdown-menu" role="menu">' +
                        '<li><a tabindex="-1" data-operate="edit_points">修改积分</a></li>' +
                        '<li><a tabindex="-1" data-operate="edit_activity_points">修改绿色积分</a></li>' +
                        '<li><a tabindex="-1" data-operate="edit_cargo_value">修改宝豆</a></li>' +
                        '<li><a tabindex="-1" data-operate="edit_group">修改身份</a></li>' +
                        // '<li><a tabindex="-1" data-operate="edit_calc_auto_status">修改计算状态</a></li>' +
                        // '<li><a tabindex="-1" data-operate="edit_calc_manual_status">修改干预状态</a></li>' +
                        // '<li><a tabindex="-1" data-operate="transfer_point">货值转换</a></li>' +
                        // '<li><a tabindex="-1" data-operate="close"><i class="fa fa-close fa-fw"></i>关闭</a></li>' +
                        // '<li><a tabindex="-1" data-operate="closeOther"><i class="fa fa-window-close-o fa-fw"></i>关闭其他</a></li>' +
                        // '<li class="divider"></li>' +
                        // '<li><a tabindex="-1" data-operate="closeAll"><i class="fa fa-power-off fa-fw"></i>关闭全部</a></li>' +
                        '</ul>' +
                        '</div>');
                    console.log($('body').find(".nav-addtabs"))
                    $('body').find(".nav-addtabs2").contextmenu({
                        target: "#context-menu2",
                        // scopes: 'li[role=presentation]',
                        onItem: function (e, event) {
                            var $element = $(event.target);
                            var id = e.data('id');
                            console.log('fsafasd', e, $element)
                            switch ($element.data('operate')) {
                                case 'edit_points':
                                    Fast.api.open('user/user/edit_points?ids=' + id)
                                    break;
                                case 'edit_activity_points':
                                    Fast.api.open('user/user/edit_activity_points?ids=' + id)
                                    break;
                                case 'edit_cargo_value':
                                    Fast.api.open('user/user/edit_cargo_value?ids=' + id)
                                    break;
                                case 'edit_group':
                                    Fast.api.open('user/user/edit_group?ids=' + id)
                                    break;
                                case 'edit_calc_manual_status':
                                    var calc_manual_status = e.data('calc_manual_status');
                                    Layer.confirm('确定要' + (calc_manual_status == 0 ? '启动' : '停滞') + '吗?', function (index) {
                                        Fast.api.ajax({ url: 'user/user/edit_calc_manual_status?ids=' + id }, function (data, ret) {
                                            $(".btn-refresh").trigger("click");
                                            Layer.closeAll();
                                        })
                                    });
                                    break;
                                case 'edit_calc_auto_status':
                                    var calc_auto_status = e.data('calc_auto_status');
                                    Layer.confirm('确定要' + (calc_auto_status == 0 ? '启动' : '停滞') + '吗?', function (index) {
                                        Fast.api.ajax({ url: 'user/user/edit_calc_auto_status?ids=' + id }, function (data, ret) {
                                            $(".btn-refresh").trigger("click");
                                            Layer.closeAll();
                                        })
                                    });
                                    break;
                                case 'transfer_point':
                                    Fast.api.ajax({ url: '/api/user/auto_transfer_point?user_id=' + id }, function (data, ret) {
                                        $(".btn-refresh").trigger("click");
                                        Layer.closeAll();
                                    })
                                    break;
                                default:
                                    break;
                            }
                        }
                    });
                });
            })



            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                fixedColumns: true,
                fixedRightNumber: 1,
                search: false,
                columns: [
                    [
                        { checkbox: true },
                        { field: 'group.name', title: __('Group') },
                        { field: 'id', title: __('Id'), sortable: true },
                        { field: 'parent_id', title: __('上级用户/ID'), formatter: function (value, row) { return (row.parent.username ? row.parent.username : '') + (row.parent_id > 0 ? '/' + row.parent_id : ''); } },
                        { field: 'username', title: __('Username'), operate: 'LIKE' },
                        { field: 'nickname', title: __('Nickname'), operate: 'LIKE' },
                        // {field: 'email', title: __('Email'), operate: 'LIKE'},
                        { field: 'mobile', title: __('Mobile'), operate: 'LIKE' },
                        { field: 'cargo_value', title: __('cargo_value'), operate: 'BETWEEN', sortable: true },
                        { field: 'points', title: __('points'), operate: 'BETWEEN', sortable: true },
                        { field: 'activity_points', title: __('activity_points'), operate: 'BETWEEN', sortable: true },
                        // { field: 'calc_type', title: __('calc_type'), searchList: { 1: __('calc_type 1'), 2: __('calc_type 2') }, sortable: true, formatter: Table.api.formatter.status },
                        // { field: 'calc_auto_status', title: __('calc_auto_status'), searchList: { 1: __('calc_auto_status 1'), 0: __('calc_auto_status 0') }, sortable: true, formatter: Table.api.formatter.status },
                        // { field: 'calc_manual_status', title: __('calc_manual_status'), searchList: { 1: __('calc_manual_status 1'), 0: __('calc_manual_status 0') }, sortable: true, formatter: Table.api.formatter.status },
                        // { field: 'cycle_endtime', title: __('cycle_endtime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true },
                        // {field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        // {field: 'level', title: __('Level'), operate: 'BETWEEN', sortable: true},
                        // {field: 'gender', title: __('Gender'), visible: false, searchList: {1: __('Male'), 0: __('Female')}},
                        // {field: 'score', title: __('Score'), operate: 'BETWEEN', sortable: true},
                        // {field: 'successions', title: __('Successions'), visible: false, operate: 'BETWEEN', sortable: true},
                        // {field: 'maxsuccessions', title: __('Maxsuccessions'), visible: false, operate: 'BETWEEN', sortable: true},
                        // {field: 'logintime', title: __('Logintime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        // {field: 'loginip', title: __('Loginip'), formatter: Table.api.formatter.search},
                        // {field: 'jointime', title: __('Jointime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        // {field: 'joinip', title: __('Joinip'), formatter: Table.api.formatter.search},
                        { field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: { normal: __('Normal'), hidden: __('Hidden') } },
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate
                            , buttons: [
                                // {
                                //     name: 'op',
                                //     text: '操作',
                                //     title: '操作',
                                //     icon: 'fa fa-user',
                                //     classname: 'btn btn-xs btn-primary nav-addtabs2',
                                //     extend: function (e) {
                                //         return 'data-id="' + e.id + '" data-calc_manual_status="' + e.calc_manual_status + '"' + '" data-calc_auto_status="' + e.calc_auto_status + '"';
                                //     }
                                //     // refresh: true,
                                //     //判断按钮是否隐藏
                                //     // hidden: function(row){
                                //     //     return row.type == 1 ? true : false
                                //     // },
                                // },
                                {
                                    name: 'erweima',
                                    text: __('二维码'),
                                    // icon: 'fa fa-list',
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: 'user/user/erweima',
                                },
                                {
                                    name: 'more',
                                    text: '更多',
                                    title: '更多',
                                    icon: 'fa fa-user',
                                    classname: 'btn btn-xs btn-primary btn-more',
                                    extend: function (e) {
                                        return 'data-id="' + e.id + '" data-calc_manual_status="' + e.calc_manual_status + '" data-calc_auto_status="' + e.calc_auto_status + '"';
                                    }
                                }
                                /*,
                                {
                                    name: 'edit_points',
                                    text: '修改积分',
                                    title: '修改积分',
                                    icon: 'fa fa-user',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    // refresh: true,
                                    //判断按钮是否隐藏
                                    // hidden: function(row){
                                    //     return row.type == 1 ? true : false
                                    // },
                                    url: 'user/user/edit_points'
                                },
                                {
                                    name: 'edit_activity_points',
                                    text: '修改活动积分',
                                    title: '修改活动积分',
                                    icon: 'fa fa-user',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    refresh: true,
                                    //判断按钮是否隐藏
                                    // hidden: function(row){
                                    //     return row.type == 1 ? true : false
                                    // },
                                    url: 'user/user/edit_activity_points'
                                },
                                {
                                    name: 'edit_cargo_value',
                                    text: '修改货值',
                                    title: '修改货值',
                                    icon: 'fa fa-user',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    refresh: true,
                                    //判断按钮是否隐藏
                                    // hidden: function(row){
                                    //     return row.type == 1 ? true : false
                                    // },
                                    url: 'user/user/edit_cargo_value'
                                },
                                {
                                    name: 'edit_group',
                                    text: '修改身份',
                                    title: '修改身份',
                                    icon: 'fa fa-user',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    refresh: true,
                                    //判断按钮是否隐藏
                                    // hidden: function(row){
                                    //     return row.type == 1 ? true : false
                                    // },
                                    url: 'user/user/edit_group'
                                },
                                {
                                    name: 'edit_calc_manual_status',
                                    text: '修改干预状态',
                                    title: '修改干预状态',
                                    icon: 'fa fa-user',
                                    classname: 'btn btn-xs btn-primary btn-ajax',
                                    confirm: function(e){
                                        return '确认要' + (e.calc_manual_status == 0 ? '启动' : '停滞') + '吗?';
                                    },
                                    refresh: true,
                                    //判断按钮是否隐藏
                                    // hidden: function(row){
                                    //     return row.type == 1 ? true : false
                                    // },
                                    url: 'user/user/edit_calc_manual_status'
                                }*/
                            ]
                        }
                    ]
                ]
            });


            // 为表格绑定事件
            Table.api.bindevent(table);

            // 添加更多按钮的下拉菜单容器
            if ($("#more-menu-container").length === 0) {
                $("body").append(
                    '<div id="more-menu-container" style="position:absolute;z-index:9999;display:none;">' +
                    '<ul class="dropdown-menu" style="display:block;position:static;margin-bottom:5px;">' +
                    '<li><a href="javascript:;" data-operate="edit_points"><i class="fa fa-edit"></i> 修改积分</a></li>' +
                    '<li><a href="javascript:;" data-operate="edit_activity_points"><i class="fa fa-edit"></i> 修改绿色积分</a></li>' +
                    '<li><a href="javascript:;" data-operate="edit_cargo_value"><i class="fa fa-edit"></i> 修改宝豆</a></li>' +
                    '<li><a href="javascript:;" data-operate="edit_group"><i class="fa fa-users"></i> 修改身份</a></li>' +
                    // '<li><a href="javascript:;" data-operate="edit_calc_auto_status"><i class="fa fa-toggle-on"></i> 修改计算状态</a></li>' +
                    // '<li><a href="javascript:;" data-operate="edit_calc_manual_status"><i class="fa fa-toggle-on"></i> 修改干预状态</a></li>' +
                    // '<li><a href="javascript:;" data-operate="transfer_point"><i class="fa fa-exchange"></i> 货值转换</a></li>' +
                    '</ul>' +
                    '</div>');
            }

            // 处理更多按钮点击事件 - 使用table.on为动态创建的元素绑定事件
            table.on("click", ".btn-more", function (e) {
                e.stopPropagation();
                e.preventDefault();

                // 获取当前行数据
                var id = $(this).data("id");
                var calc_manual_status = $(this).data("calc_manual_status");
                var calc_auto_status = $(this).data("calc_auto_status");

                // 存储当前选中的行ID到菜单容器中
                $("#more-menu-container")
                    .data("id", id)
                    .data("calc_manual_status", calc_manual_status)
                    .data("calc_auto_status", calc_auto_status);

                // 计算位置并显示菜单
                var offset = $(this).offset();
                $("#more-menu-container")
                    .css({
                        left: offset.left,
                        top: offset.top + $(this).outerHeight()
                    })
                    .show();
            });

            // 点击菜单项处理
            $(document).on("click", "#more-menu-container a", function (e) {
                e.stopPropagation();
                e.preventDefault();

                var $element = $(this);
                var id = $("#more-menu-container").data("id");
                var calc_manual_status = $("#more-menu-container").data("calc_manual_status");
                var calc_auto_status = $("#more-menu-container").data("calc_auto_status");

                switch ($element.data("operate")) {
                    case "edit_points":
                        Fast.api.open('user/user/edit_points?ids=' + id);
                        break;
                    case "edit_activity_points":
                        Fast.api.open('user/user/edit_activity_points?ids=' + id);
                        break;
                    case "edit_cargo_value":
                        Fast.api.open('user/user/edit_cargo_value?ids=' + id);
                        break;
                    case "edit_group":
                        Fast.api.open('user/user/edit_group?ids=' + id);
                        break;
                    case "edit_calc_manual_status":
                        Layer.confirm('确定要' + (calc_manual_status == 0 ? '启动' : '停滞') + '吗?', function (index) {
                            Fast.api.ajax({ url: 'user/user/edit_calc_manual_status?ids=' + id }, function (data, ret) {
                                $(".btn-refresh").trigger("click");
                                Layer.closeAll();
                            });
                        });
                        break;
                    case "edit_calc_auto_status":
                        Layer.confirm('确定要' + (calc_auto_status == 0 ? '启动' : '停滞') + '吗?', function (index) {
                            Fast.api.ajax({ url: 'user/user/edit_calc_auto_status?ids=' + id }, function (data, ret) {
                                $(".btn-refresh").trigger("click");
                                Layer.closeAll();
                            });
                        });
                        break;
                    case "transfer_point":
                        Fast.api.ajax({ url: '/api/user/auto_transfer_point?user_id=' + id }, function (data, ret) {
                            $(".btn-refresh").trigger("click");
                            Layer.closeAll();
                        });
                        break;
                }

                // 隐藏下拉菜单
                $("#more-menu-container").hide();
            });

            // 点击页面其他位置时关闭下拉菜单
            $(document).on("click", function () {
                $("#more-menu-container").hide();
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        edit_cargo_value: function () {
            Controller.api.bindevent();
        },
        edit_points: function () {
            Controller.api.bindevent();
        },
        edit_activity_points: function () {
            Controller.api.bindevent();
        },
        edit_group: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});